package com.carreirati.evaluation;

import com.carreirati.evaluation.domain.AppraisalRelationship;
import com.carreirati.evaluation.domain.AppraisalResults;
import com.carreirati.evaluation.domain.Parameter;
import com.carreirati.evaluation.domain.Question;
import com.carreirati.evaluation.domain.QuestionParameter;
import com.carreirati.evaluation.domain.QuestionParameterPK;
import com.carreirati.evaluation.domain.VwGetEvaluationResultsByQuestionParameter;

import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/appraisalresultses")
@Controller
@RooWebScaffold(path = "appraisalresultses", formBackingObject = AppraisalResults.class)
@RooWebJson(jsonObject = AppraisalResults.class)
public class AppraisalResultsController {

    @RequestMapping(value = "/find{appraisalId}", headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<java.lang.String> findJson(@PathVariable("appraisalId") long appraisalId, @RequestParam("questionId") long questionId) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<AppraisalResults> result = AppraisalResults.findAppraisalResultById(appraisalId, questionId);
        return new ResponseEntity<String>(AppraisalResults.toJsonArray(result), headers, HttpStatus.OK);
    }
    
    @RequestMapping(value = "/update{appraisalId}", headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> updateFromJson(@PathVariable("appraisalId") long appraisalId,  @RequestParam("aux") String aux, @RequestBody String json) {
        	
        String[] x = aux.split(";");
        long questionId = Long.valueOf(x[0]);
                
        long[] chosenParametersId = new long[x.length -1];    
        for (int i =1;i<x.length;i++){
        	chosenParametersId[i-1] = Long.valueOf(x[i]);	
        }
        AppraisalResults ar = new AppraisalResults();
        
        ar.save(appraisalId, questionId, chosenParametersId);
        
        
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");      
        return new ResponseEntity<String>(headers, HttpStatus.OK);        
    }
     

}
